package com.school.movie.business.web.controller;

import com.school.movie.business.web.model.vo.HallSeatVo;
import com.school.movie.business.web.model.vo.OrderVo;
import com.school.movie.business.web.model.vo.PlanVo;
import com.school.movie.business.web.model.vo.UserVo;
import com.school.movie.common.utils.BaseConstants;
import com.school.movie.common.bean.PageBean;
import com.school.movie.service.imp.OrderServiceImpl;
import com.school.movie.service.imp.SysLogServiceImp;
import com.school.movie.service.imp.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 订单控制器
 *
 * @author zhaoren
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderServiceImpl orderService;
    @Autowired
    SysLogServiceImp logService;
    @Autowired
    UserServiceImpl userService;

    @RequestMapping("/list")
    public String list(int planId, PageBean pager, HttpSession session, ModelMap map) {

        if (planId == 0) {
            if (session.getAttribute("planId") != null) {
                planId = (Integer) session.getAttribute("planId");
            }
        } else {
            session.setAttribute("planId", planId);
        }
        getParams(planId, pager, map);
        return "order";
    }

    @PostMapping("/save")
    public String save(HttpServletRequest request, HttpSession session, OrderVo order, ModelMap map) throws Exception {
        int planId = (Integer) session.getAttribute("planId");
        UserVo u = null;
        if (order.getMobile() != null) {
            u = userService.getById(order.getMobile());
        }
        if (u == null) {
            map = getParams(planId, new PageBean(), map);
            map.addAttribute("msg", "该手机号码不是注册用户，购票失败。");
            return "order";
        }
        order.setPlanId(planId);
        if (order.getOrderId() > 0) {
            orderService.update(order);
            logService.add(BaseConstants.LOG_UPDATE, order.toString());
        } else {
            orderService.add(order);
            logService.add(BaseConstants.LOG_ADD, order.toString());
        }
        map = getParams(planId, new PageBean(), map);
        return "order";
    }

    private ModelMap getParams(int planId, PageBean pager, ModelMap map) {
        int cnt = orderService.count(planId);
        pager.setTotalRows(cnt);
        List<OrderVo> movieList = orderService.list(planId, pager);
        PlanVo plan = orderService.plan(planId);
        List<HallSeatVo> seatList = orderService.seatList(plan.getHallId(), plan.getPlanId());
        pager.setData(movieList);
        map.addAttribute("pager", pager);
        map.addAttribute("order", new OrderVo());
        map.addAttribute("seatList", seatList);
        map.addAttribute("plan", plan);
        return map;
    }

    //    @RequestMapping("/update")
//    public String update(OrderVo order ,ModelMap map){
//        order = orderService.getById(order.getOrderId());
//        map.addAttribute("order",order);
//        return "order";
//    }


    @RequestMapping("/del")
    public String del(String ids, ModelMap map, HttpSession session) {
        int planId = (Integer) session.getAttribute("planId");
        String[] arr = ids.split(",");
        orderService.del(arr);
        getParams(planId, new PageBean(), map);
        return "order";
    }


}
